package com.fd.wpx.authorizating.client.session;

import com.fd.wpx.authorizating.session.RemoteSessionService;
import com.fd.wpx.authorizating.utils.SerializableUtils;
import com.fd.wpx.common.pojo.BlogResult;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.CachingSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;

public class ClientSessionDAO extends CachingSessionDAO {
    @Autowired
    private RemoteSessionService sessionService;
    @Override
    protected void doUpdate(Session session) {
        sessionService.updateSession(session.getId(), SerializableUtils.serialize(session));
    }

    @Override
    protected void doDelete(Session session) {
        sessionService.deleteSession(session.getId());
    }

    @Override
    protected Serializable doCreate(Session session) {
        Serializable sessionId = generateSessionId(session);
        assignSessionId(session, sessionId);
        sessionService.saveSession(session.getId(), SerializableUtils.serialize(session));
        return sessionId;
    }

    @Override
    protected Session doReadSession(Serializable serializable) {
        BlogResult result=sessionService.getSession(serializable);
        if(result.getStatus()==404)
        {
            return null;
        }
        String strSession=(String)result.getData();
        return SerializableUtils.deserialize(strSession);
    }
}
